<?php
namespace App\Admin\Controllers\OverSeasIn;

use App\Admin\Ajax\OverSeasIn\DropshippingReceiveAjax;
use App\Admin\Ajax\OverSeasIn\ReceiveExAjax;
use App\Models\Basics\AmazonAccount;
use App\Models\Basics\SysArea;
use App\Models\Basics\SysPlatAccount;
use App\Models\OverSeasIn\Delivery;
use App\Models\OverSeasBa\Warehouse;
use App\Models\OverSeasIn\DeliveryBox;
use App\Models\Product\ProductClassify;
use App\Models\Sys\Logs;
use Encore\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\Request;
class DropshippingReceiveExController extends AdminController{

    protected $title = 'CN至海外仓(代发)-入仓异常';

    protected $filters = [
        'start_date' => null,
        'end_date' => null,
        'delivery_no' => null,
        'batch_no' => null,
        'warehouse_id' => null,
        'area_id' => null,
        'status' => null,
        'ex_deal' => null,
        'sku' => null,
        'account_id' => null,
        'classify_id' => null,
        'fnsku' => null,
    ];

    protected function grid()
    {
        $filtersField = array_keys($this->filters);
        $filters = $this->_getFilters($filtersField);
        $this->_initFilters($filters);
        $query = $this->_initQuery($filters);
        $paginator = $this->_paginator($query);
        $action = getAction($filtersField);
        $filtersBlade = [];
        $filtersBlade['start_date'] = $this->filters['start_date'];
        $filtersBlade['end_date'] = $this->filters['end_date'];
        $filtersBlade['delivery_no'] = $this->filters['delivery_no'];
        $filtersBlade['batch_no'] = $this->filters['batch_no'];
        $filtersBlade['warehouse_id'] = getOptions(Warehouse::options(),$this->filters['warehouse_id']);
        $filtersBlade['account_id'] = getOptions(SysPlatAccount::getPlatAccountArray(),$this->filters['account_id']);
        $filtersBlade['area_id'] = getOptions(SysArea::options(),$this->filters['area_id']);
        $filtersBlade['classify_id'] = getOptions(ProductClassify::Options(),$this->filters['classify_id']);
        $filtersBlade['status'] = getOptions(DeliveryBox::$_status,$this->filters['status']);
        $filtersBlade['ex_deal'] = getOptions(DeliveryBox::$_esDeal,$this->filters['ex_deal']);
        $filtersBlade['sku'] = $this->filters['sku'];
        $filtersBlade['fnsku'] = $this->filters['fnsku'];
        return view('overseas-in.dropshipping-receive-ex.index', compact('paginator','action','filtersBlade'));
    }

    private function _getFilters($filtersField){
        $filters = [];
        foreach ($filtersField as $field){
            $filters[$field] = request($field);
        }
        return $filters;
    }

    private function _initFilters($filters){
        foreach ($filters as $field => $filter){
            $this->filters[$field] = $filter;
        }
    }

    private function _initQuery($filters){
        $query = DeliveryBox::query()
            ->where('delivery_type', Delivery::OS_DELIVERY_DROPSHIPPING) //海外仓-代发
            ->where('ex_status',3)
            ->orderBy('id', 'ASC')
            ->orderBy('ex_deal', 'ASC');
        foreach ($filters as $field => $filter){
            if(!$filter)continue;
            if($field == 'start_date'){
                $query->whereHas('delivery', function ($query) use($filter){
                    $query->where('delivered_at','>=',$filter.' 00:00:00');
                });
            }
            if($field == 'end_date'){
                $query->whereHas('delivery', function ($query) use($filter){
                    $query->where('delivered_at','<=',$filter.' 23:59:59');
                });
            }
            if($field == 'sku'){
                $query->whereHas('sku', function ($query) use($filter){
                    $query->where('psku_code','like',"%{$filter}%")
                        ->orWhere('psku_cn','like',"%{$filter}%");
                });
            }
            if($field == 'classify_id'){
                $query->whereHas('skuClassify', function ($query) use($filter){
                    $query->where('classify_id', $filter);
                });
            }
            if(in_array($field,['delivery_no','batch_no','fnsku','warehouse_id','status','ex_deal','area_id','account_id'])){
                $query->where($field,$filter);
            }
        }
        return $query;
    }

    private function _paginator($query){
        return $query->paginate((int) (request("per_page",10)))->appends(Request::input());
    }

    public function ajax(){
        $action = request('action');
        $param = request('param');
        $ajax = new DropshippingReceiveAjax();
        return $param ? call_user_func([$ajax,$action],$param) : call_user_func([$ajax,$action]);
    }

    protected function detail($deliveryNo)
    {
        $delivery = Delivery::query()->where('delivery_no',$deliveryNo)->first();
        $id = $delivery->id;
        $logs = Logs::getLogs('os-in-dropshipping-delivery',$id);
        return view('overseas-in.dropshipping-receive-ex-detail.index', compact('id','delivery','logs'));
    }
}
